package com.mofofa.maozy.info.kemailon

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.mofofa.maozy.base.BaseActivity
import com.mofofa.maozy.data.ReqSaveWalletFcFa
import com.mofofa.maozy.databinding.ActivityMaozyBindWalletOtherFcfaBinding
import com.mofofa.maozy.enums.PageTagEnum
import com.mofofa.maozy.extend.*
import com.mofofa.maozy.launch.UserStateViewModel

@SuppressLint("CustomSplashScreen")
class BindWalletTypeMtnXafActivity : BaseActivity() {

    private lateinit var mMaozyActivityMaozyBinding: ActivityMaozyBindWalletOtherFcfaBinding

    private val mUserStateViewModel by lazy {
        ViewModelProvider(this)[UserStateViewModel::class.java]
    }

    private val mWalletInfoXafViewModel by lazy {
        ViewModelProvider(this)[WalletInfoXafViewModel::class.java]
    }

    override fun onMaozyBindingMaozyViewMaozyModel(): List<ViewModel>? {
        val list = mutableListOf<ViewModel>()
        list.add(mUserStateViewModel)
        list.add(mWalletInfoXafViewModel)
        return list
    }

    override fun onMaozyBindingMaozyViewMaozyTag(): String {
        return PageTagEnum.wallet_other_fcfa.name.uppercase()
    }

    override fun onMaozyBindingMaozyContentMaozyView(): View {
        mMaozyActivityMaozyBinding = ActivityMaozyBindWalletOtherFcfaBinding.inflate(layoutInflater)
        return mMaozyActivityMaozyBinding.root
    }

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun onMaozyContentMaozyViewMaozyBinded() {
        try {
            //获取用户状态回调
            mUserStateViewModel.onMaozyFetchMaozyUserMaozyStateMaozyLiveData.observe(this) {
                if (it.isSuccess) {
                    startMaozyToMaozyNextMaozyFromMaozyPage(PageTagEnum.launch)
                }
            }

            //保存用户钱包回调
            mWalletInfoXafViewModel.onMaozySaveMaozyWalletMaozyXafMaozyLiveData.observe(this) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        //跳转展示列表
                        startMaozyToMaozyNextMaozyActivityMaozyAndMaozyFinish<WalletListAddResultXafActivity>()
                        //更新提交按键状态
                        //updateCommitStateUI()
                    }
                }
            }

            //点击保存
            mMaozyActivityMaozyBinding.submitFCFABindWalletOther.setOnClickListener {
                val formDataCompleteState =
                    isMaozyAllMaozyFormMaozyDataMaozyCompleted()
                mMaozyActivityMaozyBinding.submitFCFABindWalletOther.recordMaozyClickMaozyEvent(
                    formDataCompleteState
                )
                if (formDataCompleteState.isBlank()) {
                    //保存
                    mWalletInfoXafViewModel.saveMaozyWalletMaozyXaf(
                        ReqSaveWalletFcFa(
                            walletMobile = mMaozyActivityMaozyBinding.nivWalletOtherNumber.getMaozyContent(),
                            walletMobileConfirm = mMaozyActivityMaozyBinding.nivWalletOtherNumberConfirm.getMaozyContent(),
                            walletChannel = "OTHERS"
                        )
                    )
                }
            }

            //点击其它
            mMaozyActivityMaozyBinding.submitFCFABindWalletOtherWave.setOnClickListener {
                mMaozyActivityMaozyBinding.submitFCFABindWalletOtherWave.recordMaozyClickMaozyEvent()
                startMaozyToMaozyNextMaozyActivityMaozyAndMaozyFinish<BindWalletTypeOrangeMoneyActivity>()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun isMaozyAllMaozyFormMaozyDataMaozyCompleted(): String {
        val notFillOptions = mutableListOf<String>()
        if (!mMaozyActivityMaozyBinding.nivWalletOtherNumber.isMaozyCompleted()) {
            val gTag = mMaozyActivityMaozyBinding.nivWalletOtherNumber.getMaozySafeMaozyTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (!mMaozyActivityMaozyBinding.nivWalletOtherNumberConfirm.isMaozyCompleted()) {
            val gTag = mMaozyActivityMaozyBinding.nivWalletOtherNumberConfirm.getMaozySafeMaozyTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (notFillOptions.isEmpty()) {
            return ""
        }
        notFillOptions.add(0, "NOT_FILL")
        return notFillOptions.joinToString("#")
    }
}